#include "board.h"

/**
  * @brief  配置TIM2复用输出PWM时用到的I/O
  * @param  无
  * @retval 无
  */
static void TIM2_GPIO_Config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    
    gpio_mode_set(HEAT_PID_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, HEAT_PID_GPIO_PIN);
    gpio_output_options_set(HEAT_PID_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,HEAT_PID_GPIO_PIN);
    gpio_af_set(HEAT_PID_GPIO_PORT, TIM2_CH0_AF, HEAT_PID_GPIO_PIN);
}

/**
  * @brief  配置 TIM2 的 NVIC 中断
  * @param  无
  * @retval 无
  */
static void NVIC_Configuration(void) //静态函数仅供本文件调用
{
  nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  nvic_irq_enable(HEAT_PID_TIM_IRQn,1,0);   //WIFI串口的优先级最高
}

/**
  * @brief  配置TIM2在输出PWM情况下的工作模式
  * @param  无
  * @retval 无
  */
static void TIM2_PWMOUTPUT_Config(void)
{
    /* TIMER2 configuration: generate PWM signals with different duty cycles:
       SystemCoreClock=84000000
       TIMER0CLK = SystemCoreClock / 42000 = 2KHz */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(HEAT_PID_TIM_CLK);
    timer_deinit(HEAT_PID_TIM);

    /* TIMER2 configuration */
    timer_initpara.prescaler         = (42000-1);
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = TIM2_PWM_PERIOD;  //2s的周期
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(HEAT_PID_TIM,&timer_initpara);

     /* CH0 configuration in PWM mode */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(HEAT_PID_TIM,TIMER_CH_0,&timer_ocintpara);

    timer_channel_output_pulse_value_config(HEAT_PID_TIM,TIMER_CH_0,0);   //1s计数2000个
    timer_channel_output_mode_config(HEAT_PID_TIM,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(HEAT_PID_TIM,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(HEAT_PID_TIM,ENABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(HEAT_PID_TIM);
    timer_enable(HEAT_PID_TIM);
    timer_flag_clear(HEAT_PID_TIM,TIMER_FLAG_UP);
    timer_interrupt_enable(HEAT_PID_TIM,TIMER_INT_UP);
}

/**
  * @brief  初始化通用控制定时器定时
  * @param  无
  * @retval 无
  */
void TIM2_PWM_Configuration(void)
{
    TIM2_GPIO_Config();
    TIM2_PWMOUTPUT_Config();
    NVIC_Configuration();
}

/**
  * @brief  设置TIM2比较捕获寄存器1的值(就是改变PWM的占空比)
  * @param  dat :[0-1000]
  * @retval 无
  */
void SetPWMPulseWidth(uint32_t dat)
{
  if (dat <= TIM2_PWM_PERIOD)
  {
  /* configure TIMER channel output pulse value */
      timer_channel_output_pulse_value_config(HEAT_PID_TIM,TIMER_CH_0,dat);
  }
}

uint8_t pump_flag=0;

void HEAT_PID_TIM_IRQHandler(void)
{
  static uint8_t second_16=0;
	/* enter interrupt */
	rt_interrupt_enter();

  if(timer_interrupt_flag_get(HEAT_PID_TIM, TIMER_INT_FLAG_UP) == SET)
  { 
    ++second_16;
    if(second_cnt>=8)
    { //2*8=16s
      second_16=0;
      pump_flag=1;
    }
    timer_interrupt_flag_clear(HEAT_PID_TIM,TIMER_INT_FLAG_UP);
		/*释放信号量，通知heat_PID主线程*/
		rt_sem_release(HEAT_PID_sem);
  }

	/* leave interrupt */
	rt_interrupt_leave();
}